$(function () {
	var currPage = 1, pageSize = 10, totalCount = 0;//申请
	var currPageC = 1, pageSizeC = 10, totalCountC = 0;//承接
	let S_arr = []
	//定制申请（非定向）
	$("#applyMade").click(function () {
		sessionStorage.setItem("ISDIY", "0");
		layer.open({
			type: 2,
			title: "申请定制",
			area: ['800px', '600px'],
			content: './popupPage/applyMadePop.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			end: function () {
				let parame = {
					currPage,
					pageSize,
					createUserId:sessionStorage.getItem('UserId')
				}
				requestData(parame)
			}
		});


	})
	//定制DIY产品
	$("#applyMadeDIY").click(function () {
		sessionStorage.setItem("ISDIY", "1");
		layer.open({
			type: 2,
			title: "产品DIY",
			area: ['800px', '500px'],
			content: './popupPage/applyMadeDIYPop.html',//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no'],
			end: function () {
				let parame = {
					currPage,
					pageSize,
					createUserId:sessionStorage.getItem('UserId')
				}
				requestData(parame)
			}
		});

	})

	//定制申请列表
	function requestData(parame) {
		$.ajax({
       		timeout:30000,
			url: window.Http + "/CustomizeRequest/list",
			type: 'GET',
			headers: {
				'Authorization': "Bearer " + sessionStorage.getItem('Token')
			},
			data: parame,
			success: function (res) {
				if (res.code == 200) {
					$("#applyMadeTable tbody").empty()
					if (res.rows && res.rows.length > 0) {
						let html = "",statushtml = "",state = "正常",type = "",herfUrl = "";
						res.rows.forEach(function (val, index) {
							if (val.goodsType == 1) {
								herfUrl = "./productDetail.html?serverId=" + val.goodsId + "&goodsType=" + val.goodsType;
							} else {
								herfUrl = "./productDetail.html?directoryId=" + val.goodsId + "&goodsType=" + val.goodsType;
							}
							if (val.type === 1) {
								type = "非定向定制";
							} else {
								type = "定向定制";
							}
							switch (val.status || 0) {
								case -1:
									state = "保存";
									statushtml = `<td>
													<a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a>
													<a class="layui-btn layui-btn-xs layui-btn-normal text-white mr-2 edit">修改</a>
													<a class="layui-btn layui-btn-xs layui-btn-danger text-white mr-2 operate" style="display:inline-block">删除</a></td>`;
									break;
								case -2:
									state = "审核失败";
									statushtml = `<td>
													<a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a>
													<a class="layui-btn layui-btn-xs layui-btn-danger text-white mr-2 operate" style="display:inline-block">删除</a>
													</td>`;
									break;
								case 0:
									state = "正常上架";
									statushtml = `<td>
													<a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a>
													<a class="layui-btn layui-btn-xs layui-btn-normal text-white mr-2 edit">修改</a>
													<a class="layui-btn layui-btn-xs layui-btn-danger text-white mr-2 operate" style="display:inline-block">删除</a></td>`;
									break;
								case 2:
									state = "待审核";
									statushtml = `<td>
													<a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a>
													<a class="layui-btn layui-btn-xs layui-btn-danger text-white mr-2 operate" style="display:inline-block">删除</a>
													<a class="layui-btn layui-btn-xs layui-btn-normal text-white mr-2 edit">修改</a>
													</td>`;
									break;
								case 1:
									state = "已接单";
									statushtml = `<td>
													<a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a></td>`
									break;
								default:
									break;
							}

							html += `<tr dataId = "` + val.id + `" dataStatus = "${val.status}">
									<th scope="row">`+ (index + 1 + (parame.currPage - 1) * parame.pageSize) + `</th>
									<td>`+ (val.name||"—") + `</td>
									<td>`+ type + `</td>
									<td>`+ state + `</td>
									<td>`+ val.createBy + `</td>
									<td>`+ val.createTime + `</td>
									<td>`+ `${val.acceptUserName ? val.acceptUserName:'—'}` + `</td>
									`+ statushtml + `
								</tr>`
							S_arr.push(val.id)
						})
						$("#applyMadeTable tbody").append(html);
					} else {
						let html = `<tr><td scope="row" colspan="8" style="text-align:center">暂无数据</td></tr>`
						$("#applyMadeTable tbody").append(html);
					}
					totalCount = res.total;
					if (currPage == 1) {
						//执行一个laypage实例
						layui.laypage.render({
							elem: 'applyMadeTablePage', //注意，这里的 test1 是 ID，不用加 # 号
							count: totalCount, //数据总数，从服务端得到
							limit: 10,
							groups: 5,
							jump: function (obj, first) {
								if (!first) {
									currPage = obj.curr;
									let parame = {
										currPage,
										pageSize,
										createUserId:sessionStorage.getItem('UserId')
									}
									requestData(parame);
								}
							}
						});
					}

				}

			}
		})
	}

	//承接定制列表
	function AcceptRequestData(parame) {
		$.ajax({
       		timeout:30000,
			url: window.Http + "/CustomizeRequest/list",
			type: 'GET',
			headers: {
				'Authorization': "Bearer " + sessionStorage.getItem('Token')
			},
			data: parame,
			success: function (res) {
				if (res.code == 200) {
					$("#claimMadeTable tbody").empty()
					if (res.rows && res.rows.length > 0) {
						let html = "",statushtml = "";
						let state='无'
						statushtml=`<td><a class="layui-btn layui-btn-xs text-white mr-2 info">详情</a></td>`
						res.rows.forEach(function (val, index) {
							switch (val.isClaimStatus) {
								case 1:
									state='待审核'
									break;
								case 2:
									state='审核通过'
									break;
								case 0:
									state='审核失败'
									break;
								default:
									layer.msg('状态错误')
									break;
							}
							html += `<tr dataId = "` + val.id + `" dataStatus = "${val.status}">
									<th scope="row">`+ (index + 1) + `</th>
									<td>`+ val.name + `</td>
									<td>`+ state + `</td>
									<td>`+ val.createBy + `</td>
									<td>`+ val.createTime + `</td>
									`+ statushtml + `
								</tr>`
						})
						$("#claimMadeTable tbody").append(html);
					} else {
						let html = `<tr><td scope="row" colspan="8" style="text-align:center">暂无数据</td></tr>`
						$("#claimMadeTable tbody").append(html);
					}
					totalCount = res.total;
					if (currPage == 1) {
						//执行一个laypage实例
						layui.laypage.render({
							elem: 'claimMadeTablePage', //注意，这里的 test1 是 ID，不用加 # 号
							count: totalCount, //数据总数，从服务端得到
							limit: 10,
							groups: 5,
							jump: function (obj, first) {
								if (!first) {
									currPage = obj.curr;
									let parame = {
										currPage:currPageC,
										pageSize:pageSizeC,
										createUserId:sessionStorage.getItem('UserId')
									}
									AcceptRequestData(parame);
								}
							}
						});
					}

				}

			}
		})
	}

	(function () {
		let parame = {
			currPage,
			pageSize,
			createUserId:sessionStorage.getItem('UserId'),
		}
		requestData(parame);

		let parame2 = {
			currPage,
			pageSize,
			// createUserId:sessionStorage.getItem('UserId'),
			isClaim:1
		}
		AcceptRequestData(parame2);
	}());

	// 申请撤销操作
	$("#applyMadeTable tbody").delegate('.operate', 'click', function () {
		console.log('点击撤销',)
		let id = $(this).parents('[dataId]').attr('dataId')
		let status = $(this).parents('[dataStatus]').attr('dataStatus')
		$.ajax({
       		timeout:30000,
			url: window.Http + "/CustomizeRequest/"+Number(id),
			type: 'DELETE',
			headers: {
				'Authorization': "Bearer " + sessionStorage.getItem('Token')
			},
			success: function (res) {
				if (res.code ===200) {
					layer.alert(`${res.msg}`, {
						icon: 1,
						title: ""
					});
					let parame = {
						currPage,
						pageSize,
						createUserId:sessionStorage.getItem('UserId')
					}
					requestData(parame)
				}
			}
		})
	})

	// 申请修改操作
	$("#applyMadeTable tbody").delegate('.edit', 'click', function () {
		let id = $(this).parents('[dataId]').attr('dataId')
		window.ApplyMadePopId = id;
		layer.open({
			type: 2,
			title: "修改定制",
			area: ['800px', '600px'],
			content: `./popupPage/editApplyMadePop.html`, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			success: function (layero, index) {
				console.log(layero, index);
				console.log($(this))
				return id
			},
			end: function () {
				let parame = {
					currPage,
					pageSize,
					createUserId:sessionStorage.getItem('UserId')
				}
				requestData(parame)
			}
		});

	})

	// 点击详情跳转
	$("#applyMadeTable tbody").delegate('.info','click',function(){
		let id = $(this).parents('[dataId]').attr('dataId')
		window.location.href = './customDetial.html?customId='+ id
	})
	// 点击详情跳转
	$("#claimMadeTable tbody").delegate('.info','click',function(){
		let id = $(this).parents('[dataId]').attr('dataId')
		window.location.href = './customDetial.html?customId='+ id
	})

})
